home *** CD-ROM | disk | FTP | other *** search
/ CD Actual Thematic 7: Programming / CDAT7.iso / demos / VisualAge for Java 2.0 Entry / setup / data1.cab / ide-e / IDE / cache / 13S04FH (.txt) < prev    next >
Encoding:
Java Class File  |  1998-09-16  |  10.9 KB  |  359 lines

  1. package com.sun.java.swing;
  2.  
  3. import com.sun.java.accessibility.Accessible;
  4. import com.sun.java.accessibility.AccessibleContext;
  5. import com.sun.java.accessibility.AccessibleState;
  6. import com.sun.java.swing.event.ChangeEvent;
  7. import com.sun.java.swing.event.ChangeListener;
  8. import com.sun.java.swing.event.EventListenerList;
  9. import com.sun.java.swing.plaf.SliderUI;
  10. import java.awt.Component;
  11. import java.util.Dictionary;
  12. import java.util.Enumeration;
  13. import java.util.Hashtable;
  14.  
  15. public class JSlider extends JComponent implements SwingConstants, Accessible {
  16.    protected BoundedRangeModel sliderModel;
  17.    protected int majorTickSpacing;
  18.    protected int minorTickSpacing;
  19.    protected boolean snapToTicks;
  20.    private boolean paintTicks;
  21.    private boolean paintLabels;
  22.    private boolean isInverted;
  23.    protected int orientation;
  24.    private Dictionary labelTable;
  25.    protected ChangeListener changeListener;
  26.    protected transient ChangeEvent changeEvent;
  27.    static Class class$com$sun$java$swing$event$ChangeListener;
  28.  
  29.    public JSlider() {
  30.       this(0, 0, 100, 50);
  31.    }
  32.  
  33.    public JSlider(int orientation, int min, int max, int value) {
  34.       this.snapToTicks = true;
  35.       this.paintTicks = false;
  36.       this.paintLabels = false;
  37.       this.isInverted = false;
  38.       this.changeListener = this.createChangeListener();
  39.       this.changeEvent = null;
  40.       this.checkOrientation(orientation);
  41.       this.orientation = orientation;
  42.       this.sliderModel = new DefaultBoundedRangeModel(value, 0, min, max);
  43.       this.sliderModel.addChangeListener(this.changeListener);
  44.       this.updateUI();
  45.    }
  46.  
  47.    public void addChangeListener(ChangeListener l) {
  48.       EventListenerList var10000 = super.listenerList;
  49.       Class var10001 = class$com$sun$java$swing$event$ChangeListener;
  50.       if (var10001 == null) {
  51.          try {
  52.             var10001 = Class.forName("com.sun.java.swing.event.ChangeListener");
  53.          } catch (ClassNotFoundException var2) {
  54.             throw new NoClassDefFoundError(((Throwable)var2).getMessage());
  55.          }
  56.  
  57.          class$com$sun$java$swing$event$ChangeListener = var10001;
  58.       }
  59.  
  60.       var10000.add(var10001, l);
  61.    }
  62.  
  63.    private void checkOrientation(int orientation) {
  64.       switch (orientation) {
  65.          case 0:
  66.          case 1:
  67.             return;
  68.          default:
  69.             throw new IllegalArgumentException("orientation must be one of: VERTICAL, HORIZONTAL");
  70.       }
  71.    }
  72.  
  73.    protected ChangeListener createChangeListener() {
  74.       return new ModelListener(this);
  75.    }
  76.  
  77.    public Hashtable createStandardLabels(int increment) {
  78.       return this.createStandardLabels(increment, this.getMinimum());
  79.    }
  80.  
  81.    public Hashtable createStandardLabels(int increment, int start) {
  82.       if (start <= this.getMaximum() && start >= this.getMinimum()) {
  83.          Hashtable table = new Hashtable();
  84.  
  85.          for(int labelIndex = start; labelIndex <= this.getMaximum(); labelIndex += increment) {
  86.             table.put(new Integer(labelIndex), new JLabel("" + labelIndex, 0));
  87.          }
  88.  
  89.          return table;
  90.       } else {
  91.          throw new IllegalArgumentException("Slider label start point out of range.");
  92.       }
  93.    }
  94.  
  95.    protected void fireStateChanged() {
  96.       Object[] listeners = super.listenerList.getListenerList();
  97.  
  98.       for(int i = listeners.length - 2; i >= 0; i -= 2) {
  99.          Object var10000 = listeners[i];
  100.          Class var10001 = class$com$sun$java$swing$event$ChangeListener;
  101.          if (var10001 == null) {
  102.             try {
  103.                var10001 = Class.forName("com.sun.java.swing.event.ChangeListener");
  104.             } catch (ClassNotFoundException var3) {
  105.                throw new NoClassDefFoundError(((Throwable)var3).getMessage());
  106.             }
  107.  
  108.             class$com$sun$java$swing$event$ChangeListener = var10001;
  109.          }
  110.  
  111.          if (var10000 == var10001) {
  112.             if (this.changeEvent == null) {
  113.                this.changeEvent = new ChangeEvent(this);
  114.             }
  115.  
  116.             ((ChangeListener)listeners[i + 1]).stateChanged(this.changeEvent);
  117.          }
  118.       }
  119.  
  120.    }
  121.  
  122.    public AccessibleContext getAccessibleContext() {
  123.       if (super.accessibleContext == null) {
  124.          super.accessibleContext = new AccessibleJSlider(this);
  125.       }
  126.  
  127.       return super.accessibleContext;
  128.    }
  129.  
  130.    public int getExtent() {
  131.       return this.getModel().getExtent();
  132.    }
  133.  
  134.    public boolean getInverted() {
  135.       return this.isInverted;
  136.    }
  137.  
  138.    public Dictionary getLabelTable() {
  139.       return this.labelTable;
  140.    }
  141.  
  142.    public int getMajorTickSpacing() {
  143.       return this.majorTickSpacing;
  144.    }
  145.  
  146.    public int getMaximum() {
  147.       return this.getModel().getMaximum();
  148.    }
  149.  
  150.    public int getMinimum() {
  151.       return this.getModel().getMinimum();
  152.    }
  153.  
  154.    public int getMinorTickSpacing() {
  155.       return this.minorTickSpacing;
  156.    }
  157.  
  158.    public BoundedRangeModel getModel() {
  159.       return this.sliderModel;
  160.    }
  161.  
  162.    public int getOrientation() {
  163.       return this.orientation;
  164.    }
  165.  
  166.    public boolean getPaintLabels() {
  167.       return this.paintLabels;
  168.    }
  169.  
  170.    public boolean getPaintTicks() {
  171.       return this.paintTicks;
  172.    }
  173.  
  174.    public boolean getSnapToTicks() {
  175.       return this.snapToTicks;
  176.    }
  177.  
  178.    public SliderUI getUI() {
  179.       return (SliderUI)super.ui;
  180.    }
  181.  
  182.    public String getUIClassID() {
  183.       return "SliderUI";
  184.    }
  185.  
  186.    public int getValue() {
  187.       return this.getModel().getValue();
  188.    }
  189.  
  190.    public boolean getValueIsAdjusting() {
  191.       return this.getModel().getValueIsAdjusting();
  192.    }
  193.  
  194.    public void removeChangeListener(ChangeListener l) {
  195.       EventListenerList var10000 = super.listenerList;
  196.       Class var10001 = class$com$sun$java$swing$event$ChangeListener;
  197.       if (var10001 == null) {
  198.          try {
  199.             var10001 = Class.forName("com.sun.java.swing.event.ChangeListener");
  200.          } catch (ClassNotFoundException var2) {
  201.             throw new NoClassDefFoundError(((Throwable)var2).getMessage());
  202.          }
  203.  
  204.          class$com$sun$java$swing$event$ChangeListener = var10001;
  205.       }
  206.  
  207.       var10000.remove(var10001, l);
  208.    }
  209.  
  210.    public void setExtent(int extent) {
  211.       this.getModel().setExtent(extent);
  212.    }
  213.  
  214.    public void setInverted(boolean b) {
  215.       boolean oldValue = this.isInverted;
  216.       this.isInverted = b;
  217.       ((JComponent)this).firePropertyChange("inverted", oldValue, this.isInverted);
  218.    }
  219.  
  220.    public void setLabelTable(Dictionary labels) {
  221.       Dictionary oldTable = this.labelTable;
  222.       this.labelTable = labels;
  223.       this.updateLabelUIs();
  224.       ((JComponent)this).firePropertyChange("labelTable", oldTable, this.labelTable);
  225.    }
  226.  
  227.    public void setMajorTickSpacing(int n) {
  228.       int oldValue = this.majorTickSpacing;
  229.       this.majorTickSpacing = n;
  230.       if (this.labelTable == null && this.getMajorTickSpacing() > 0 && this.getPaintLabels()) {
  231.          this.setLabelTable(this.createStandardLabels(this.getMajorTickSpacing()));
  232.       }
  233.  
  234.       ((JComponent)this).firePropertyChange("majorTickSpacing", oldValue, this.majorTickSpacing);
  235.    }
  236.  
  237.    public void setMaximum(int maximum) {
  238.       this.getModel().setMaximum(maximum);
  239.    }
  240.  
  241.    public void setMinimum(int minimum) {
  242.       this.getModel().setMinimum(minimum);
  243.    }
  244.  
  245.    public void setMinorTickSpacing(int n) {
  246.       int oldValue = this.minorTickSpacing;
  247.       this.minorTickSpacing = n;
  248.       ((JComponent)this).firePropertyChange("minorTickSpacing", oldValue, this.minorTickSpacing);
  249.    }
  250.  
  251.    public void setModel(BoundedRangeModel newModel) {
  252.       BoundedRangeModel oldModel = this.getModel();
  253.       if (oldModel != null) {
  254.          oldModel.removeChangeListener(this.changeListener);
  255.       }
  256.  
  257.       this.sliderModel = newModel;
  258.       if (newModel != null) {
  259.          newModel.addChangeListener(this.changeListener);
  260.          if (super.accessibleContext != null) {
  261.             super.accessibleContext.firePropertyChange("AccessibleValue", oldModel == null ? null : new Integer(oldModel.getValue()), newModel == null ? null : new Integer(newModel.getValue()));
  262.          }
  263.       }
  264.  
  265.       ((JComponent)this).firePropertyChange("model", oldModel, this.sliderModel);
  266.    }
  267.  
  268.    public void setOrientation(int orientation) {
  269.       this.checkOrientation(orientation);
  270.       this.orientation = orientation;
  271.       ((JComponent)this).firePropertyChange("orientation", orientation, orientation);
  272.       if (orientation != orientation && super.accessibleContext != null) {
  273.          super.accessibleContext.firePropertyChange("AccessibleState", orientation == 1 ? AccessibleState.VERTICAL : AccessibleState.HORIZONTAL, orientation == 1 ? AccessibleState.VERTICAL : AccessibleState.HORIZONTAL);
  274.       }
  275.  
  276.    }
  277.  
  278.    public void setPaintLabels(boolean b) {
  279.       boolean oldValue = this.paintLabels;
  280.       this.paintLabels = b;
  281.       if (this.labelTable == null && this.getMajorTickSpacing() > 0) {
  282.          this.setLabelTable(this.createStandardLabels(this.getMajorTickSpacing()));
  283.       }
  284.  
  285.       ((JComponent)this).firePropertyChange("paintLabels", oldValue, this.paintLabels);
  286.    }
  287.  
  288.    public void setPaintTicks(boolean b) {
  289.       boolean oldValue = this.paintTicks;
  290.       this.paintTicks = b;
  291.       ((JComponent)this).firePropertyChange("paintTicks", oldValue, this.paintTicks);
  292.    }
  293.  
  294.    public void setSnapToTicks(boolean b) {
  295.       boolean oldValue = this.snapToTicks;
  296.       this.snapToTicks = b;
  297.       ((JComponent)this).firePropertyChange("snapToTicks", oldValue, this.snapToTicks);
  298.    }
  299.  
  300.    public void setUI(SliderUI ui) {
  301.       super.setUI(ui);
  302.    }
  303.  
  304.    public void setValue(int n) {
  305.       BoundedRangeModel m = this.getModel();
  306.       int oldValue = m.getValue();
  307.       m.setValue(n);
  308.       if (super.accessibleContext != null) {
  309.          super.accessibleContext.firePropertyChange("AccessibleValue", new Integer(oldValue), new Integer(m.getValue()));
  310.       }
  311.  
  312.    }
  313.  
  314.    public void setValueIsAdjusting(boolean b) {
  315.       BoundedRangeModel m = this.getModel();
  316.       boolean oldValue = m.getValueIsAdjusting();
  317.       m.setValueIsAdjusting(b);
  318.       if (oldValue != b && super.accessibleContext != null) {
  319.          super.accessibleContext.firePropertyChange("AccessibleState", oldValue ? AccessibleState.BUSY : null, b ? AccessibleState.BUSY : null);
  320.       }
  321.  
  322.    }
  323.  
  324.    public String toString() {
  325.       String containerString = "";
  326.       if (!((Component)this).isEnabled() && !((Component)this).isVisible()) {
  327.          containerString = "(not Enabled, not Visible)";
  328.       } else if (!((Component)this).isEnabled()) {
  329.          containerString = "(not Enabled)";
  330.       } else if (!((Component)this).isVisible()) {
  331.          containerString = "(not Visible)";
  332.       }
  333.  
  334.       String sliderString = containerString + (this.getOrientation() == 1 ? "vertical" : "horizontal") + ", " + "value=" + this.getValue() + ", " + "adj=" + this.getValueIsAdjusting() + ", " + "min=" + this.getMinimum() + ", " + "max=" + this.getMaximum() + ", " + "majorTickSpacing=" + this.getMajorTickSpacing() + ", " + "minorTickSpacing=" + this.getMinorTickSpacing() + ", " + "snapToTicks=" + this.getSnapToTicks() + ", " + "isInverted=" + this.getInverted() + ", " + "paintLabels=" + this.getPaintLabels() + ", " + "paintTicks=" + this.getPaintTicks();
  335.       return this.getClass().getName() + "[" + sliderString + "]";
  336.    }
  337.  
  338.    protected void updateLabelUIs() {
  339.       if (this.getLabelTable() != null) {
  340.          Enumeration labels = this.getLabelTable().keys();
  341.  
  342.          while(labels.hasMoreElements()) {
  343.             Object value = this.getLabelTable().get(labels.nextElement());
  344.             if (value instanceof JComponent) {
  345.                JComponent component = (JComponent)value;
  346.                component.updateUI();
  347.                ((Component)component).setSize(component.getPreferredSize());
  348.             }
  349.          }
  350.  
  351.       }
  352.    }
  353.  
  354.    public void updateUI() {
  355.       this.updateLabelUIs();
  356.       this.setUI((SliderUI)UIManager.getUI(this));
  357.    }
  358. }
  359.